﻿namespace Beetle
{
    using Smark.Core;
    using System;
    using System.Collections;
    using System.Collections.Generic;

    public class ChannelController
    {
        private static Hashtable _field_a = new Hashtable(0x20);
        public AsyncDelegate<TcpChannel, object> Receive;

        private void a(PacketRecieveMessagerArgs A_0)
        {
            object message = A_0.Message;
            if (_field_a.Count == 0)
            {
                if (this.Receive != null)
                {
                    this.Receive(A_0.Channel, message);
                }
            }
            else
            {
                Type type = message.GetType();
                Class_i _i = (Class_i) _field_a[type];
                if (_i != null)
                {
                    _i.d(A_0.Channel, message);
                }
                else if (this.Receive != null)
                {
                    this.Receive(A_0.Channel, message);
                }
            }
        }

        public void Register(Package package)
        {
            package.ReceiveMessage = new EventPacketRecievMessage(this.a);
        }

        public static void RegisterHandler(IMessageHandler handler)
        {
            if (handler != null)
            {
                lock (_field_a)
                {
                    IList<Class_i> list = Class_i.d(handler);
                    if (list.Count > 0)
                    {
                        for (int i = 0; i < list.Count; i++)
                        {
                            Class_i _i = list[i];
                            if (!_field_a.ContainsKey(_i._field_c))
                            {
                                _field_a.Add(_i._field_c, _i);
                            }
                            else
                            {
                                _field_a[_i._field_c] = _i;
                            }
                        }
                    }
                }
            }
        }
    }
}

